package com.meixiang.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;

import com.meixiang.R;
import com.meixiang.main.MainActivity;

/**
 * 欢迎页面(闪屏页)
 *
 * @author LZRUI
 */
public class WelcomeActivity extends AppCompatActivity {

    private Handler handler;
    private Runnable jumpRunnable;

    private static final int REQUEST_PERMISSION = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN); // 隐藏信号栏
        setContentView(R.layout.activity_welcome);
        requestPermission();
    }

    private void intentUi(long delayMillis) {
        jumpRunnable = new Runnable() {

            @Override
            public void run() {
                Intent intent = new Intent();
                intent.setClass(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        };
        handler = new Handler();
        handler.postDelayed(jumpRunnable, delayMillis);
    }

    private void requestPermission() {
        boolean writeStorage = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
        if (Build.VERSION.SDK_INT < 23 || writeStorage) {
            intentUi(3000);
        } else {
            ActivityCompat.requestPermissions(this
                    , new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}
                    , REQUEST_PERMISSION);
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == REQUEST_PERMISSION) {

            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                intentUi(2000);
            } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) {
                    ActivityCompat.requestPermissions(this, new String[]{permissions[0]}, REQUEST_PERMISSION);
                } else {

                    new AlertDialog.Builder(this).setTitle("温馨提示")
                            .setMessage("为了能正常使用" + getString(R.string.app_name) + ",请开启存储卡权限")
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    finish();
                                }
                            })
                            .setPositiveButton("手动授权", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    Uri uri = Uri.parse("package:" + getPackageName());
                                    Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS", uri);
                                    startActivity(intent);
                                    finish();
                                }
                            }).show();

                }
            }
        }


    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacks(jumpRunnable);
        super.onDestroy();
    }
}
